package com.example.rockscissorspapergame.service;


/**
 * Page与Service通信的帮助类
 * 利用静态内部类实现的单例模式帮助类
 */
public class ConnectionHelper {

    /**
     * 私有构造方法，避免单例被额外实例化
     */
    private ConnectionHelper() {}

    /**
     * 静态的内部类，持有唯一的 ConnectionHelper 实例
     */
    private static class ConnectionHelperHolder {
        private static final ConnectionHelper INSTANCE = new ConnectionHelper();
    }

    /**
     * 对外提供获取实例的方法
     *
     * @return ConnectionHelper 实例
     */
    public static ConnectionHelper getInstance() {
        return ConnectionHelperHolder.INSTANCE;
    }

    /**
     * 通信回调
     */
    private IConnectionCallback mConnectionCallback;

    /**
     * 设置通信回调
     *
     * @param connectionCallback 通信回调
     */
    public void setCallback(IConnectionCallback connectionCallback) {
        this.mConnectionCallback = connectionCallback;
    }

    /**
     * 发送消息
     *
     * @param message 消息
     */
    public void sendMessage(String message) {
        if (mConnectionCallback != null) {
            mConnectionCallback.onCallback(message);
        }
    }
}

